package cn.itlaobing.web.dao;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.itlaobing.web.model.ListModel;
import cn.itlaobing.web.model.PaginatoinModel;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

public class ListDao extends BaseDao{
	private DBhelper db=new DBhelper();
	public List<ListModel> queryOne(Integer userid) throws SQLException,IOException{
		String sql="select * from tbl_list where userid=?";
		ResultSet rs=db.executeQuery(sql,userid);
		List<ListModel> list=new ArrayList<ListModel>();
		while(rs.next()) {
			ListModel lm=new ListModel();
			/*lm.setAttutide(rs.getString("attutide"));
			lm.setId(rs.getInt("id"));
			lm.setListfrom(rs.getString("listfrom"));*/
			lm.setListid(rs.getString("listid"));
			lm.setSums(rs.getDouble("sums"));
			lm.setThistime(rs.getTimestamp("thistime"));
			lm.setToaddress(rs.getString("toaddress"));
			lm.setToname(rs.getString("toname"));
			lm.setTotele(rs.getString("totele"));
			lm.setUserid(rs.getInt("userid"));
			list.add(lm);
		}
		return list;
	}
	public int updateOne(String attitude,String listid) throws IOException, SQLException {
		String sql="update tbl_list set attutide=? where listid=?";
		return db.executeUpdate(sql,attitude,listid);
	}
	public void close() throws SQLException{
		db.close();
	}
	public PaginatoinModel<ListModel> Pagination(Integer thisPage,Integer size,Integer userid) throws SQLException {
		PaginatoinModel<ListModel> page=new PaginatoinModel<>();
		page.setThisPage(thisPage);
		page.setSize(size);
		QueryRunner qr=new QueryRunner(this.getDataSource());
		String sql="select * from tbl_list where userid=? limit ?,?";
		BeanListHandler<ListModel> beanListHandler=new BeanListHandler<ListModel>(ListModel.class);
		List<ListModel> lm=qr.query(sql,beanListHandler,userid,(thisPage-1)*size,size);
		sql="select count(1) from tbl_list where userid=?";
		ScalarHandler<Long> scalarHandler=new ScalarHandler<>();
		Long nums=qr.query(sql,scalarHandler,userid);
		page.setTotalsize(nums.intValue());
		page.setListModels(lm);
		return page;
	}
}
